// const request = require('request');
// const querystring = require('querystring');

function fun(jsonObject, data, states) {
    var html = '';
    const DriverName = jsonObject[0].DriverName;
    const FleetCName = jsonObject[0].FleetCName;
    const PhoneNum = jsonObject[0].PhoneNum;
    const TruckLic = jsonObject[0].TruckLic;
    if (states == 0) { //进
        enterresult = entertimes(jsonObject);
        html += `
        <div id="top">
            <div id="top_icon">
                <img src="">
            </div>
            <div id="name">
                <div id="DriverName">
                    ${DriverName}
                </div>
                <div id="FleetCName">
                    ${FleetCName}   
                </div>
            </div>
            <div id="phone_exit">
                <div id="phonenum">
                    手机：${PhoneNum}
                </div>
                <div id="exit">
                    <a href="http://www.baidu.com">退出--》</a>
                </div>
            </div>
        </div>
        <div id="middle">
            <div id="TruckLic">
                ${TruckLic}
            </div>
            <div id="times_bt">
                <div id="times">
                    进 ${enterresult[0]} 次
                </div>
                <div id="button">
                    <button type="button" onclick="use('${0}','${enterresult[1]}','${DriverName}','${FleetCName}','${PhoneNum}','${TruckLic}')">去使用</button>
                </div>
            </div>
            <div id="text">
                注意：点击"去使用"之后请在10分钟之内通行！
            </div>
        </div>
        `;
    } else {
        outresult = outtimes(jsonObject);
        html += `
        <div id="top">
            <div id="top_icon">
                <img src="">
            </div>
            <div id="name">
                <div id="DriverName">
                    ${DriverName}
                </div>
                <div id="FleetCName">
                    ${FleetCName}   
                </div>
            </div>
            <div id="phone_exit">
                <div id="phonenum">
                    手机：${PhoneNum}
                </div>
                <div id="exit">
                    <a href="http://www.baidu.com">退出--》</a>
                </div>
            </div>
        </div>
        <div id="middle">
            <div id="TruckLic">
                ${TruckLic}
            </div>
            <div id="times_bt">
                <div id="times">
                    出 ${outresult[0]} 次
                </div>
                <div id="button">
                    <button type="button" onclick="use('${1}','${outresult[1]}','${DriverName}','${FleetCName}','${PhoneNum}','${TruckLic}')">去使用</button>
                </div>
            </div>
            <div id="text">
                注意：点击"去使用"之后请在10分钟之内通行！
            </div>
        </div>
        `;
    }
    return data.replace('芜湖港——szx', html);
}

function entertimes(jsonObject) {
    var entertime = 0;
    var flag = 0;
    var Couponid;
    var result = new Array();
    jsonObject.forEach(function(item) {
        if (item.StatusNow == 'A') {
            entertime++;
            if (flag == 0) {
                Couponid = item.CouponID;
                flag = 1;
            }
        } else if (item.StatusNow == 2) {
            entertime++;
            if (flag == 0) {
                Couponid = item.CouponID;
                flag = 1;
            }
        }
    });
    result[0] = entertime;
    result[1] = Couponid;
    return result;
}

function outtimes(jsonObject) {
    var outtime = 0;
    var flag = 0;
    var Couponid;
    var result = new Array();
    jsonObject.forEach(function(item) {
        if (item.StatusNow == 'B') {
            outtime++;
            if (flag == 0) {
                Couponid = item.CouponID;
                flag = 1;
            }
        } else if (item.StatusNow == 2) {
            outtime++;
            if (flag == 0) {
                Couponid = item.CouponID;
                flag = 1;
            }
        }
    });
    result[0] = outtime;
    result[1] = Couponid;
    return result;
}

function use(Direction, CouponID, DriverName, FleetCName, PhoneNum, TruckLic) {
    // alert(DriverName);
    var told = confirm("确定使用嘛？");
    if (told) {
        const HTTP = new XMLHttpRequest();
        data = {
            "Direction": Direction,
            "CouponID": CouponID
        };
        const URL = "https://bf5eab07-7aec-4ab0-9665-ad65527c9144.mock.pstmn.io?Direction=" + Direction + "&CouponID=" + CouponID;
        HTTP.open("GET", URL);
        HTTP.send();
        HTTP.onreadystatechange = (e) => {
            if (HTTP.status == 200) {
                window.location.href = '/static/html/use.html?DriverName=' + encodeURI(encodeURI(DriverName)) + '&FleetCName=' + encodeURI(encodeURI(FleetCName)) + '&PhoneNum=' + encodeURI(encodeURI(PhoneNum)) + '&TruckLic=' + encodeURI(encodeURI(TruckLic));
            } else {
                alert('使用失败！');
            }
        }
    }
}